<?php
defined('IN_ZEEGING') or die('Access Denied');
require_once ZEEGING_PATH.'Librarys'.DIRECTORY_SEPARATOR.'ethereum'.DIRECTORY_SEPARATOR.'EthereumApi.php';
class DepositOrderModel extends Model{
	/*
	CREATE TABLE `transactions` (
	  `txhash` varchar(200) NOT NULL COMMENT '交易hash',
	  `blockhash` varchar(200) NOT NULL,
	  `blockheight` varchar(50) NOT NULL,
	  `receiveraddress` varchar(200) NOT NULL COMMENT '接受人的地址',
	  `receiverid` varchar(200) NOT NULL COMMENT '接收人的id',
	  `value` varchar(200) NOT NULL COMMENT '交易货币价值',
	  `rmbamount` float NOT NULL COMMENT '交易时等值人民币金额',
	  `currency` varchar(50) NOT NULL COMMENT '币名称',
	  `status` int(4) NOT NULL DEFAULT 0 COMMENT '状态：0初始状态，1充值成功，-1遇到问题',
	  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
	  `time` datetime NOT NULL DEFAULT current_timestamp(),
	  PRIMARY KEY (`id`),
	  UNIQUE KEY `txhash` (`txhash`)
	) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='交易记录'
	*/
	private $row;
	const ORDERSTATUS_INIT=0;
	const ORDERSTATUS_SUCCESS=1;
	const ORDERSTATUS_ERROR=-1;

	public function __construct(){
		$this->row=(object)array();
		parent::__construct();
	}
	public function save(){
		$txhash=$this->row->txhash;
		$blockhash=$this->row->blockhash;
		$blockheight=$this->row->blockheight;
		$receiveraddress=$this->row->receiveraddress;
		$receiverid=$this->row->receiverid;
		$value=$this->row->value;
		$rmbamount=$this->row->rmbamount;
		$currency=$this->row->currency;
		$insertSql="INSERT INTO `transactions`(
				`txhash`, `blockhash`, `blockheight`, `receiveraddress`, `receiverid`, `value`, `rmbamount`, `currency`,`status`)
				VALUES ('$txhash', '$blockhash', '$blockheight', '$receiveraddress', '$receiverid', '$value', $rmbamount, '$currency', 0)";
		$id=($this->execute($insertSql) ? $this->fetch($this->query('select @@IDENTITY;')) : null );
		if($id>0){
			$this->row->id=$id[0];
			return true;
		}
		else{
			return false;
		}
	}
	public function success(){
		if(!isset($this->row->id))return false;
		$id=$this->row->id;
		$updateSql="UPDATE `transactions` SET `status`=1 WHERE `id`=$id";
		return $res=$this->execute($updateSql)? true : false;
	}

	public function __get($name){
		return isset($this->row->$name)? $this->row->$name : false;
	}
	public function __set($name,$value){
		return $this->row->$name=$value;
	}
}